home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / m68k / cc68k.arc / EXPR.C < prev    next >
C/C++ Source or Header  |  1986-10-26  |  38KB  |  984 lines

  1. #include        "stdio.h"
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or question
  18.  
  19.  *    to:
  20.  *
  21.  *        Matthew Brandt
  22.  *        Box 920337
  23.  *        Norcross, Ga 30092
  24.  */
  25.  
  26. TYP             stdint = { bt_long, 0, 4, {0, 0}, 0, 0 };
  27. TYP             stdchar = {bt_char, 0, 1, {0, 0}, 0, 0 };
  28. TYP             stdstring = {bt_pointer, 1, 4, {0, 0}, &stdchar, 0};
  29. TYP             stdfunc = {bt_func, 1, 0, {0, 0}, &stdint, 0};
  30. extern TYP      *head;          /* shared with decl */
  31.  
  32. /*
  33.  *      expression evaluation
  34.  *
  35.  *      this set of routines builds a parse tree for an expression.
  36.  *      no code is generated for the expressions during the build,
  37.  *      this is the job of the codegen module. for most purposes
  38.  *      expression() is the routine to call. it will allow all of
  39.  *      the C operators. for the case where the comma operator is
  40.  *      not valid (function parameters for instance) call exprnc().
  41.  *
  42.  *      each of the routines returns a pointer to a describing type
  43.  *      structure. each routine also takes one parameter which is a
  44.  *      pointer to an expression node by reference (address of pointer).
  45.  *      the completed expression is returned in this pointer. all
  46.  *      routines return either a pointer to a valid type or NULL if
  47.  *      the hierarchy of the next operator is too low or the next
  48.  *      symbol is not part of an expression.
  49.  */
  50.  
  51. TYP     *expression();  /* forward declaration */
  52. TYP     *exprnc();      /* forward declaration */
  53. TYP     *unary();       /* forward declaration */
  54.  
  55. struct enode    *makenode(nt, v1, v2)
  56. /*
  57.  *      build an expression node with a node type of nt and values
  58.  *      v1 and v2.
  59.  */
  60. int                 nt;
  61. struct enode            *v1, *v2;
  62. {       struct enode    *ep;
  63.         ep = (struct enode *)xalloc(sizeof(struct enode));
  64.         ep->nodetype = nt;
  65.         ep->constflag = 0;
  66.         ep->v.p[0] = v1;
  67.         ep->v.p[1] = v2;
  68.         return ep;
  69. }
  70.  
  71. TYP *deref(node,tp)
  72. /*
  73.  *      build the proper dereference operation for a node using the
  74.  *      type pointer tp.
  75.  */
  76. struct enode    **node;
  77. TYP             *tp;
  78. {       switch( tp->type ) {
  79.                 case bt_char:
  80.                         *node = makenode(en_b_ref,*node,(struct enode *)0);
  81.                         tp = &stdint;
  82.                         break;
  83.                 case bt_short:
  84.                 case bt_enum:
  85.                         *node = makenode(en_w_ref,*node,(struct enode *)0);
  86.                         tp = &stdint;
  87.                         break;
  88.                 case bt_long:
  89.                 case bt_pointer:
  90.                 case bt_unsigned:
  91.                         *node = makenode(en_l_ref,*node,(struct enode *)0);
  92.                         break;
  93.                 default:
  94.                         error(ERR_DEREF);
  95.                         break;
  96.                 }
  97.         return tp;
  98. }
  99.  
  100. TYP     *nameref(node)
  101. /*
  102.  *      nameref will build an expression tree that references an
  103.  *      identifier. if the identifier is not in the global or
  104.  *      local symbol table then a look-ahead to the next character
  105.  *      is done and if it indicates a function call the identifier
  106.  *      is coerced to an external function name. non-value references
  107.  *      generate an additional level of indirection.
  108.  */
  109. struct enode    **node;
  110. {       SYM             *sp;
  111.         TYP             *tp;
  112.         sp =(SYM *)gsearch(lastid);
  113.         if( sp == 0 ) {
  114.                 while( isspace(lastch) )
  115.                         getch();
  116.                 if( lastch == '(') {
  117.                         ++global_flag;
  118.                         sp =(SYM *)xalloc(sizeof(SYM));
  119.                         sp->tp = &stdfunc;
  120.                         sp->name =(char *)litlate(lastid);
  121.                         sp->storage_class = sc_external;
  122.                         insert(sp,&gsyms);
  123.                         --global_flag;
  124.                         tp = &stdfunc;
  125.                         *node = makenode(en_nacon,(struct enode *)sp->name,
  126.                                                   (struct enode *)0);
  127.                         (*node)->constflag = 1;
  128.                         }
  129.                 else    {
  130.                         tp = 0;
  131.                         error(ERR_UNDEFINED);
  132.                         }
  133.                 }
  134.         else    {
  135.                 if( (tp = sp->tp) == 0 ) {
  136.                         error(ERR_UNDEFINED);
  137.                         return 0;       /* guard against untyped entries */
  138.                         }
  139.                 switch( sp->storage_class ) {
  140.                         case sc_static:
  141.                                 *node = makenode(en_labcon,
  142.                                                 (struct enode *)sp->value.i,
  143.                                                 (struct enode *)0);
  144.                                 (*node)->constflag = 1;
  145.                                 break;
  146.                         case sc_global:
  147.                         case sc_external:
  148.                                 *node = makenode(en_nacon,
  149.                                                 (struct enode *)sp->name,
  150.                                                 (struct enode *)0);
  151.                                 (*node)->constflag = 1;
  152.                                 break;
  153.                         case sc_const:
  154.                                 *node = makenode(en_icon,
  155.                                                 (struct enode *)sp->value.i,
  156.                                                 (struct enode *)0);
  157.                                 (*node)->constflag = 1;
  158.                                 break;
  159.                         default:        /* auto and any errors */
  160.                                 if( sp->storage_class != sc_auto)
  161.                                         error(ERR_ILLCLASS);
  162.                                 *node = makenode(en_autocon,
  163.                                                 (struct enode *)sp->value.i,
  164.                                                 (struct enode *)0);
  165.                                 break;
  166.                         }
  167.                 if( tp->val_flag == 0)
  168.                         tp = (TYP *)deref(node,tp);
  169.                 }
  170.         getsym();
  171.         return tp;
  172. }
  173.  
  174. struct enode    *parmlist()
  175. /*
  176.  *      parmlist will build a list of parameter expressions in
  177.  *      a function call and return a pointer to the last expression
  178.  *      parsed. since parameters are generally pushed from right
  179.  *      to left we get just what we asked for...
  180.  */
  181. {       struct enode    *ep1, *ep2;
  182.         ep1 = 0;
  183.         while( lastst != closepa) {
  184.                 exprnc( &ep2);          /* evaluate a parameter */
  185.                 ep1 = makenode(en_void,ep2,ep1);
  186.                 if( lastst != comma)
  187.                         break;
  188.                 getsym();
  189.                 }
  190.         return ep1;
  191. }
  192.  
  193. int     castbegin(st)
  194. /*
  195.  *      return 1 if st in set of [ kw_char, kw_short, kw_long, kw_int,
  196.  *      kw_float, kw_double, kw_struct, kw_union ]
  197.  */
  198. int     st;
  199. {       return  st == kw_char || st == kw_short || st == kw_int ||
  200.                 st == kw_long || st == kw_float || st == kw_double ||
  201.                 st == kw_struct || st == kw_union || st== kw_unsigned;
  202. }
  203.  
  204. TYP     *primary(node)
  205. /*
  206.  *      primary will parse a primary expression and set the node pointer
  207.  *      returning the type of the expression parsed. primary expressions
  208.  *      are any of:
  209.  *                      id
  210.  *                      constant
  211.  *                      string
  212.  *                      ( expression )
  213.  *                      primary[ expression ]
  214.  *                      primary.id
  215.  *                      primary->id
  216.  *                      primary( parameter list )
  217.  */
  218. struct enode    **node;
  219. {       struct enode    *pnode, *qnode, *rnode;
  220.         SYM             *sp;
  221.         TYP             *tptr;
  222.         TABLE           *dummyptr;
  223.         switch( lastst ) {
  224.  
  225.                 case id:
  226.                         tptr = nameref(&pnode);
  227.                         break;
  228.                 case iconst:
  229.                         tptr = &stdint;
  230.                         pnode = makenode(en_icon,(struct enode *)ival,
  231.                                         (struct enode *)0);
  232.                         pnode->constflag = 1;
  233.                         getsym();
  234.                         break;
  235.                 case sconst:
  236.                         tptr = &stdstring;
  237.                         pnode = makenode(en_labcon,
  238.                                         (struct enode *)stringlit(laststr),
  239.                                         (struct enode *)0);
  240.                         pnode->constflag = 1;
  241.                         getsym();
  242.                         break;
  243.                 case openpa:
  244.                         getsym();
  245.                         if( !castbegin(lastst) ) {
  246.                                 tptr = expression(&pnode);
  247.                                 needpunc(closepa);
  248.                                 }
  249.                         else    {       /* cast operator */
  250.                                 dummyptr = (TABLE *)0;
  251.                                 decl(dummyptr); /* do cast declaration */
  252.                                 decl1();
  253.                                 tptr = head;
  254.                                 needpunc(closepa);
  255.                                 if( unary(&pnode) == 0 ) {
  256.                                         error(ERR_IDEXPECT);
  257.                                         tptr = 0;
  258.                                         }
  259.                                 }
  260.                         break;
  261.                 default:
  262.                         return 0;
  263.                 }
  264.         for(;;) {
  265.                 switch( lastst ) {
  266.                         case openbr:    /* build a subscript reference */
  267.                                 if( tptr->type != bt_pointer )
  268.                                         error(ERR_NOPOINTER);
  269.                                 else
  270.                                         tptr = tptr->btp;
  271.                                 getsym();
  272.                                 qnode = makenode(en_icon,
  273.                                                 (struct enode *)tptr->size,
  274.                                                 (struct enode *)0);
  275.                                 qnode->constflag = 1;
  276.                                 expression(&rnode);
  277. /*
  278.  *      we could check the type of the expression here...
  279.  */
  280.                                 qnode = makenode(en_mul,qnode,rnode);
  281.                                 qnode->constflag = rnode->constflag &&
  282.                                         qnode->v.p[0]->constflag;
  283.                                 pnode = makenode(en_add,qnode,pnode);
  284.                                 pnode->constflag = qnode->constflag &&
  285.                                         pnode->v.p[1]->constflag;
  286.                                 if( tptr->val_flag == 0 )
  287.                                         tptr =(TYP *)deref(&pnode,tptr);
  288.                                 needpunc(closebr);
  289.                                 break;
  290.                         case pointsto:
  291.                                 if( tptr->type != bt_pointer )
  292.                                         error(ERR_NOPOINTER);
  293.                                 else
  294.                                         tptr = tptr->btp;
  295.                                 if( tptr->val_flag == 0 )
  296.                                         pnode = makenode(en_l_ref,pnode,
  297.                                                         (struct enode *)0);
  298. /*
  299.  *      fall through to dot operation
  300.  */
  301.                         case dot:
  302.                                 getsym();       /* past -> or . */
  303.                                 if( lastst != id )
  304.                                         error(ERR_IDEXPECT);
  305.                                 else    {
  306.                                         sp =(SYM *)search(lastid,tptr->lst.head);
  307.                                         if( sp == 0 )
  308.                                                 error(ERR_NOMEMBER);
  309.                                         else    {
  310.                                                 tptr = sp->tp;
  311.                                                 qnode = makenode(en_icon,(struct enode *)sp->value.i,
  312.                                                                 (struct enode *)0);
  313.                                                 qnode->constflag = 1;
  314.                                                 pnode = makenode(en_add,pnode,qnode);
  315.                                                 pnode->constflag = pnode->v.p[0]->constflag;
  316.                                                 if( tptr->val_flag == 0 )
  317.                                                     tptr =(TYP *) deref(&pnode,tptr);
  318.                                                 }
  319.                                         getsym();       /* past id */
  320.                                         }
  321.                                 break;
  322.                         case openpa:    /* function reference */
  323.                                 getsym();
  324.                                 if( tptr->type != bt_func &&
  325.                                         tptr->type != bt_ifunc )
  326.                                         error(ERR_NOFUNC);
  327.                                 else
  328.                                         tptr = tptr->btp;
  329.                                 pnode = makenode(en_fcall,pnode,parmlist());
  330.                                 needpunc(closepa);
  331.                                 break;
  332.                         default:
  333.                                 goto fini;
  334.                         }
  335.                 }
  336. fini:   *node = pnode;
  337.         return tptr;
  338. }
  339.  
  340. int     lvalue(node)
  341. /*
  342.  *      this function returns true if the node passed is an lvalue.
  343.  *      this can be qualified by the fact that an lvalue must have
  344.  *      one of the dereference operators as it's top node.
  345.  */
  346. struct enode    *node;
  347. {       switch( node->nodetype ) {
  348.                 case en_b_ref:
  349.                 case en_w_ref:
  350.                 case en_l_ref:
  351.                 case en_ub_ref:
  352.                 case en_uw_ref:
  353.                 case en_ul_ref:
  354.                         return 1;
  355.                 case en_cbl:
  356.                 case en_cwl:
  357.                 case en_cbw:
  358.                         return lvalue(node->v.p[0]);
  359.                 }
  360.         return 0;
  361. }
  362.  
  363. TYP     *unary(node)
  364. /*
  365.  *      unary evaluates unary expressions and returns the type of the
  366.  *      expression evaluated. unary expressions are any of:
  367.  *
  368.  *                      primary
  369.  *                      primary++
  370.  *                      primary--
  371.  *                      !unary
  372.  *                      ~unary
  373.  *                      ++unary
  374.  *                      --unary
  375.  *                      -unary
  376.  *                      *unary
  377.  *                      &unary
  378.  *                      (typecast)unary
  379.  *                      sizeof(typecast)
  380.  *
  381.  */
  382. struct enode    **node;
  383. {       TYP             *tp, *tp1;
  384.         struct enode    *ep1, *ep2;
  385.         int             flag, i;
  386.         TABLE           *dummyptr;
  387.         flag = 0;
  388.         switch( lastst ) {
  389.                 case autodec:
  390.                         flag = 1;
  391.                 /* fall through to common increment */
  392.                 case autoinc:
  393.                         getsym();
  394.                         tp = unary(&ep1);
  395.                         if( tp == 0 ) {
  396.                                 error(ERR_IDEXPECT);
  397.                                 return 0;
  398.                                 }
  399.                         if( lvalue(ep1)) {
  400.                                 if( tp->type == bt_pointer )
  401.                                         ep2 = makenode(en_icon,
  402.                                                       (struct enode *)tp->btp->size,
  403.                                                       (struct enode *)0);
  404.                                  else
  405.                                         ep2 = makenode(en_icon,
  406.                                                       (struct enode *)1,
  407.                                                       (struct enode *)0);
  408.                                 ep2->constflag = 1;
  409.                                 ep1 = makenode(flag ? en_assub : en_asadd,ep1,ep2);
  410.                                 }
  411.                         else
  412.                                 error(ERR_LVALUE);
  413.                         break;
  414.                 case minus:
  415.                         getsym();
  416.                         tp = unary(&ep1);
  417.                         if( tp == 0 ) {
  418.                                 error(ERR_IDEXPECT);
  419.                                 return 0;
  420.                                 }
  421.                         ep1 = makenode(en_uminus,ep1,(struct enode *)0);
  422.                         ep1->constflag = ep1->v.p[0]->constflag;
  423.                         break;
  424.                 case not:
  425.                         getsym();
  426.                         tp = unary(&ep1);
  427.                         if( tp == 0 ) {
  428.                                 error(ERR_IDEXPECT);
  429.                                 return 0;
  430.                                 }
  431.                         ep1 = makenode(en_not,ep1,(struct enode *)0);
  432.                         ep1->constflag = ep1->v.p[0]->constflag;
  433.                         break;
  434.                 case compl:
  435.                         getsym();
  436.                         tp = unary(&ep1);
  437.                         if( tp == 0 ) {
  438.                                 error(ERR_IDEXPECT);
  439.                                 return 0;
  440.                                 }
  441.                         ep1 = makenode(en_compl,ep1,(struct enode *)0);
  442.                         ep1->constflag = ep1->v.p[0]->constflag;
  443.                         break;
  444.                 case star:
  445.                         getsym();
  446.                         tp = unary(&ep1);
  447.                         if( tp == 0 ) {
  448.                                 error(ERR_IDEXPECT);
  449.                                 return 0;
  450.                                 }
  451.                         if( tp->btp == 0 )
  452.                                 error(ERR_DEREF);
  453.                         else
  454.                                 tp = tp->btp;
  455.                         if( tp->val_flag == 0 )
  456.                                 tp =(TYP *) deref(&ep1,tp);
  457.                         break;
  458.                 case and:
  459.                         getsym();
  460.                         tp = unary(&ep1);
  461.                         if( tp == 0 ) {
  462.                                 error(ERR_IDEXPECT);
  463.                                 return 0;
  464.                                 }
  465.                         if( lvalue(ep1))
  466.                                 ep1 = ep1->v.p[0];
  467.                         tp1 = (TYP *)xalloc(sizeof(TYP));
  468.                         tp1->size = 4;
  469.                         tp1->type = bt_pointer;
  470.                         tp1->btp = tp;
  471.                         tp1->val_flag = 0;
  472.                         tp1->lst.head = 0;
  473.                         tp1->sname = 0;
  474.                         tp = tp1;
  475.                         break;
  476.                 case kw_sizeof:
  477.                         getsym();
  478.                         needpunc(openpa);
  479.                         dummyptr = (TABLE *) 0;
  480.                         decl(dummyptr);
  481.                         decl1();
  482.                         if( head != 0 )
  483.                                 ep1 = makenode(en_icon,
  484.                                               (struct enode *)head->size,
  485.                                               (struct enode *)0);
  486.                         else    {
  487.                                 error(ERR_IDEXPECT);
  488.                                 ep1 = makenode(en_icon,(struct enode *)1,
  489.                                               (struct enode *)0);
  490.                                 }
  491.                         ep1->constflag = 1;
  492.                         tp = &stdint;
  493.                         needpunc(closepa);
  494.                         break;
  495.                 default:
  496.                         tp = primary(&ep1);
  497.                         if( tp != 0 ) {
  498.                                 if( tp->type == bt_pointer )
  499.                                         i = (int)tp->btp->size;
  500.                                 else
  501.                                         i = 1;
  502.                                 if( lastst == autoinc) {
  503.                                         if( lvalue(ep1) )
  504.                                                 ep1 = makenode(en_ainc,ep1,
  505.                                                               (struct enode *)i);
  506.                                         else
  507.                                                 error(ERR_LVALUE);
  508.                                         getsym();
  509.                                         }
  510.                                 else if( lastst == autodec ) {
  511.                                         if( lvalue(ep1) )
  512.                                                 ep1 = makenode(en_adec,ep1,
  513.                                                               (struct enode *)i);
  514.                                         else
  515.                                                 error(ERR_LVALUE);
  516.                                         getsym();
  517.                                         }
  518.                                 }
  519.                         break;
  520.                 }
  521.         *node = ep1;
  522.         return tp;
  523. }
  524.  
  525. TYP     *forcefit(tp1,tp2)
  526. /*
  527.  *      forcefit will coerce the nodes passed into compatable
  528.  *      types and return the type of the resulting expression.
  529.  */
  530. TYP             *tp1, *tp2;
  531. {       switch( tp1->type ) {
  532.                 case bt_char:
  533.                 case bt_short:
  534.                 case bt_long:
  535.                         if( tp2->type == bt_long ||
  536.                                 tp2->type == bt_short ||
  537.                                 tp2->type == bt_char)
  538.                                 return &stdint;
  539.                         else if( tp2->type == bt_pointer ||
  540.                                 tp2->type == bt_unsigned )
  541.                                 return tp2;
  542.                         break;
  543.                 case bt_pointer:
  544.                         if( isscalar(tp2) || tp2->type == bt_pointer)
  545.                                 return tp1;
  546.                         break;
  547.                 case bt_unsigned:
  548.                         if( tp2->type == bt_pointer )
  549.                                 return tp2;
  550.                         else if( isscalar(tp2) )
  551.                                 return tp1;
  552.                         break;
  553.                 }
  554.         error( ERR_MISMATCH );
  555.         return tp1;
  556. }
  557.  
  558. int     isscalar(tp)
  559. /*
  560.  *      this function returns true when the type of the argument is
  561.  *      one of char, short, unsigned, or long.
  562.  */
  563. TYP             *tp;
  564. {       return  tp->type == bt_char ||
  565.                 tp->type == bt_short ||
  566.                 tp->type == bt_long ||
  567.                 tp->type == bt_unsigned;
  568. }
  569.  
  570. TYP     *multops(node)
  571. /*
  572.  *      multops parses the multiply priority operators. the syntax of
  573.  *      this group is:
  574.  *
  575.  *              unary
  576.  *              multop * unary
  577.  *              multop / unary
  578.  *              multop % unary
  579.  */
  580. struct enode    **node;
  581. {       struct enode    *ep1, *ep2;
  582.         TYP             *tp1, *tp2;
  583.         int              oper;
  584.         tp1 = unary(&ep1);
  585.         if( tp1 == 0 )
  586.                 return 0;
  587.         while( lastst == star || lastst == divide || lastst == modop ) {
  588.                 oper = lastst;
  589.                 getsym();       /* move on to next unary op */
  590.                 tp2 = unary(&ep2);
  591.                 if( tp2 == 0 ) {
  592.                         error(ERR_IDEXPECT);
  593.                         *node = ep1;
  594.                         return tp1;
  595.                         }
  596.                 tp1 = forcefit(tp1,tp2);
  597.                 switch( oper ) {
  598.                         case star:
  599.                                 if( tp1->type == bt_unsigned )
  600.                                         ep1 = makenode(en_umul,ep1,ep2);
  601.                                 else
  602.                                         ep1 = makenode(en_mul,ep1,ep2);
  603.                                 break;
  604.                         case divide:
  605.                                 if( tp1->type == bt_unsigned )
  606.                                         ep1 = makenode(en_udiv,ep1,ep2);
  607.                                 else
  608.                                         ep1 = makenode(en_div,ep1,ep2);
  609.                                 break;
  610.                         case modop:
  611.                                 if( tp1->type == bt_unsigned )
  612.                                         ep1 = makenode(en_umod,ep1,ep2);
  613.                                 else
  614.                                         ep1 = makenode(en_mod,ep1,ep2);
  615.                                 break;
  616.                         }
  617.                 ep1->constflag = ep1->v.p[0]->constflag &&
  618.                         ep1->v.p[1]->constflag;
  619.                 }
  620.         *node = ep1;
  621.         return tp1;
  622. }
  623.  
  624. TYP     *addops(node)
  625. /*
  626.  *      addops handles the addition and subtraction operators.
  627.  */
  628. struct enode    **node;
  629. {       struct enode    *ep1, *ep2, *ep3;
  630.         TYP             *tp1, *tp2;
  631.         int             oper;
  632.         tp1 = multops(&ep1);
  633.         if( tp1 == 0 )
  634.                 return 0;
  635.         while( lastst == plus || lastst == minus ) {
  636.                 oper = (lastst == plus);
  637.                 getsym();
  638.                 tp2 = multops(&ep2);
  639.                 if( tp2 == 0 ) {
  640.                         error(ERR_IDEXPECT);
  641.                         *node = ep1;
  642.                         return tp1;
  643.                         }
  644.                 if( tp1->type == bt_pointer ) {
  645.                         tp2 = forcefit(&stdint,tp2);
  646.                         ep3 = makenode(en_icon,
  647.                                       (struct enode *)tp1->btp->size,
  648.                                       (struct enode *)0);
  649.                         ep3->constflag = 1;
  650.                         ep2 = makenode(en_mul,ep3,ep2);
  651.                         ep2->constflag = ep2->v.p[1]->constflag;
  652.                         }
  653.                 else if( tp2->type == bt_pointer ) {
  654.                         tp1 = forcefit(&stdint,tp1);
  655.                         ep3 = makenode(en_icon,
  656.                                       (struct enode *)tp2->btp->size,
  657.                                       (struct enode *)0);
  658.                         ep3->constflag = 1;
  659.                         ep1 = makenode(en_mul,ep3,ep1);
  660.                         ep1->constflag = ep1->v.p[1]->constflag;
  661.                         }
  662.                 tp1 = forcefit(tp1,tp2);
  663.                 ep1 = makenode( oper ? en_add : en_sub,ep1,ep2);
  664.                 ep1->constflag = ep1->v.p[0]->constflag &&
  665.                         ep1->v.p[1]->constflag;
  666.                 }
  667.         *node = ep1;
  668.         return tp1;
  669. }
  670.  
  671. TYP     *shiftop(node)
  672. /*
  673.  *      shiftop handles the shift operators << and >>.
  674.  */
  675. struct enode    **node;
  676. {       struct enode    *ep1, *ep2;
  677.         TYP             *tp1, *tp2;
  678.         int             oper;
  679.         tp1 = addops(&ep1);
  680.         if( tp1 == 0)
  681.                 return 0;
  682.         while( lastst == lshift || lastst == rshift) {
  683.                 oper = (lastst == lshift);
  684.                 getsym();
  685.                 tp2 = addops(&ep2);
  686.                 if( tp2 == 0 )
  687.                         error(ERR_IDEXPECT);
  688.                 else    {
  689.                         tp1 = forcefit(tp1,tp2);
  690.                         ep1 = makenode(oper ? en_lsh : en_rsh,ep1,ep2);
  691.                         ep1->constflag = ep1->v.p[0]->constflag &&
  692.                                 ep1->v.p[1]->constflag;
  693.                         }
  694.                 }
  695.         *node = ep1;
  696.         return tp1;
  697. }
  698.  
  699. TYP     *relation(node)
  700. /*
  701.  *      relation handles the relational operators < <= > and >=.
  702.  */
  703. struct enode    **node;
  704. {       struct enode    *ep1, *ep2;
  705.         TYP             *tp1, *tp2;
  706.         int             nt;
  707.         tp1 = shiftop(&ep1);
  708.         if( tp1 == 0 )
  709.                 return 0;
  710.         for(;;) {
  711.                 switch( lastst ) {
  712.  
  713.                         case lt:
  714.                                 if( tp1->type == bt_unsigned )
  715.                                         nt = en_ult;
  716.                                 else
  717.                                         nt = en_lt;
  718.                                 break;
  719.                         case gt:
  720.                                 if( tp1->type == bt_unsigned )
  721.                                         nt = en_ugt;
  722.                                 else
  723.                                         nt = en_gt;
  724.                                 break;
  725.                         case leq:
  726.                                 if( tp1->type == bt_unsigned )
  727.                                         nt = en_ule;
  728.                                 else
  729.                                         nt = en_le;
  730.                                 break;
  731.                         case geq:
  732.                                 if( tp1->type == bt_unsigned )
  733.                                         nt = en_uge;
  734.                                 else
  735.                                         nt = en_ge;
  736.                                 break;
  737.                         default:
  738.                                 goto fini;
  739.                         }
  740.                 getsym();
  741.                 tp2 = shiftop(&ep2);
  742.                 if( tp2 == 0 )
  743.                         error(ERR_IDEXPECT);
  744.                 else    {
  745.                         tp1 = forcefit(tp1,tp2);
  746.                         ep1 = makenode(nt,ep1,ep2);
  747.                         ep1->constflag = ep1->v.p[0]->constflag &&
  748.                                 ep1->v.p[1]->constflag;
  749.                         }
  750.                 }
  751. fini:   *node = ep1;
  752.         return tp1;
  753. }
  754.  
  755. TYP     *equalops(node)
  756. /*
  757.  *      equalops handles the equality and inequality operators.
  758.  */
  759. struct enode    **node;
  760. {       struct enode    *ep1, *ep2;
  761.         TYP             *tp1, *tp2;
  762.         int             oper;
  763.         tp1 = relation(&ep1);
  764.         if( tp1 == 0 )
  765.                 return 0;
  766.         while( lastst == eq || lastst == neq ) {
  767.                 oper = (lastst == eq);
  768.                 getsym();
  769.                 tp2 = relation(&ep2);
  770.                 if( tp2 == 0 )
  771.                         error(ERR_IDEXPECT);
  772.                 else    {
  773.                         tp1 = forcefit(tp1,tp2);
  774.                         ep1 = makenode( oper ? en_eq : en_ne,ep1,ep2);
  775.                         ep1->constflag = ep1->v.p[0]->constflag &&
  776.                                 ep1->v.p[1]->constflag;
  777.                         }
  778.                 }
  779.         *node = ep1;
  780.         return tp1;
  781. }
  782.  
  783. TYP     *binop(node,xfunc,nt,sy)
  784. /*
  785.  *      binop is a common routine to handle all of the legwork and
  786.  *      error checking for bitand, bitor, bitxor, andop, and orop.
  787.  */
  788. struct enode    **node;
  789. TYP             *(*xfunc)();
  790. int             nt, sy;
  791. {       struct enode    *ep1, *ep2;
  792.         TYP             *tp1, *tp2;
  793.         tp1 = (*xfunc)(&ep1);
  794.         if( tp1 == 0 )
  795.                 return 0;
  796.         while( lastst == sy ) {
  797.                 getsym();
  798.                 tp2 = (*xfunc)(&ep2);
  799.                 if( tp2 == 0 )
  800.                         error(ERR_IDEXPECT);
  801.                 else    {
  802.                         tp1 = forcefit(tp1,tp2);
  803.                         ep1 = makenode(nt,ep1,ep2);
  804.                         ep1->constflag = ep1->v.p[0]->constflag &&
  805.                                 ep1->v.p[1]->constflag;
  806.                         }
  807.                 }
  808.         *node = ep1;
  809.         return tp1;
  810. }
  811.  
  812. TYP     *bitand(node)
  813. /*
  814.  *      the bitwise and operator...
  815.  */
  816. struct enode    **node;
  817. {       return binop(node,equalops,en_and,and);
  818. }
  819.  
  820. TYP     *bitxor(node)
  821. struct enode    **node;
  822. {       return binop(node,bitand,en_xor,uparrow);
  823. }
  824.  
  825. TYP     *bitor(node)
  826. struct enode    **node;
  827. {       return binop(node,bitxor,en_or,or);
  828. }
  829.  
  830. TYP     *andop(node)
  831. struct enode    **node;
  832. {       return binop(node,bitor,en_land,land);
  833. }
  834.  
  835. TYP     *orop(node)
  836. struct enode    **node;
  837. {       return binop(node,andop,en_lor,lor);
  838. }
  839.  
  840. TYP     *conditional(node)
  841. /*
  842.  *      this routine processes the hook operator.
  843.  */
  844. struct enode    **node;
  845. {       TYP             *tp1, *tp2, *tp3;
  846.         struct enode    *ep1, *ep2, *ep3;
  847.         tp1 = orop(&ep1);       /* get condition */
  848.         if( tp1 == 0 )
  849.                 return 0;
  850.         if( lastst == hook ) {
  851.                 getsym();
  852.                 if( (tp2 = conditional(&ep2)) == 0) {
  853.                         error(ERR_IDEXPECT);
  854.                         goto cexit;
  855.                         }
  856.                 needpunc(colon);
  857.                 if( (tp3 = conditional(&ep3)) == 0) {
  858.                         error(ERR_IDEXPECT);
  859.                         goto cexit;
  860.                         }
  861.                 tp1 = forcefit(tp2,tp3);
  862.                 ep2 = makenode(en_void,ep2,ep3);
  863.                 ep1 = makenode(en_cond,ep1,ep2);
  864.                 }
  865. cexit:  *node = ep1;
  866.         return tp1;
  867. }
  868.  
  869. TYP     *asnop(node)
  870. /*
  871.  *      asnop handles the assignment operators. currently only the
  872.  *      simple assignment is implemented.
  873.  */
  874. struct enode    **node;
  875. {       struct enode    *ep1, *ep2, *ep3;
  876.         TYP             *tp1, *tp2;
  877.         int             op;
  878.         tp1 = conditional(&ep1);
  879.         if( tp1 == 0 )
  880.                 return 0;
  881.         for(;;) {
  882.                 switch( lastst ) {
  883.                         case assign:
  884.                                 op = en_assign;
  885. ascomm:                         getsym();
  886.                                 tp2 = asnop(&ep2);
  887. ascomm2:                        if( tp2 == 0 || !lvalue(ep1) )
  888.                                         error(ERR_LVALUE);
  889.                                 else    {
  890.                                         tp1 = forcefit(tp1,tp2);
  891.                                         ep1 = makenode(op,ep1,ep2);
  892.                                         }
  893.                                 break;
  894.                         case asplus:
  895.                                 op = en_asadd;
  896. ascomm3:                        tp2 = asnop(&ep2);
  897.                                 if( tp1->type == bt_pointer ) {
  898.                                         ep3 = makenode(en_icon,
  899.                                                       (struct enode *)tp1->btp->size,
  900.                                                       (struct enode *)0)
  901. ;
  902.                                         ep2 = makenode(en_mul,ep2,ep3);
  903.                                         }
  904.                                 goto ascomm;
  905.                         case asminus:
  906.                                 op = en_assub;
  907.                                 goto ascomm3;
  908.                         case astimes:
  909.                                 op = en_asmul;
  910.                                 goto ascomm;
  911.                         case asdivide:
  912.                                 op = en_asdiv;
  913.                                 goto ascomm;
  914.                         case asmodop:
  915.                                 op = en_asmod;
  916.                                 goto ascomm;
  917.                         case aslshift:
  918.                                 op = en_aslsh;
  919.                                 goto ascomm;
  920.                         case asrshift:
  921.                                 op = en_asrsh;
  922.                                 goto ascomm;
  923.                         case asand:
  924.                                 op = en_asand;
  925.                                 goto ascomm;
  926.                         case asor:
  927.                                 op = en_asor;
  928.                                 goto ascomm;
  929.                         default:
  930.                                 goto asexit;
  931.                         }
  932.                 }
  933. asexit: *node = ep1;
  934.         return tp1;
  935. }
  936.  
  937. TYP     *exprnc(node)
  938. /*
  939.  *      evaluate an expression where the comma operator is not legal.
  940.  */
  941. struct enode    **node;
  942. {       TYP     *tp;
  943.         tp = asnop(node);
  944.         if( tp == 0 )
  945.                 *node = 0;
  946.         return tp;
  947. }
  948.  
  949. TYP     *commaop(node)
  950. /*
  951.  *      evaluate the comma operator. comma operators are kept as
  952.  *      void nodes.
  953.  */
  954. struct enode    **node;
  955. {       TYP             *tp1;
  956.         struct enode    *ep1, *ep2;
  957.         tp1 = asnop(&ep1);
  958.         if( tp1 == 0 )
  959.                 return 0;
  960.         if( lastst == comma ) {
  961.                 tp1 = commaop(&ep2);
  962.                 if( tp1 == 0 ) {
  963.                         error(ERR_IDEXPECT);
  964.                         goto coexit;
  965.                         }
  966.                 ep1 = makenode(en_void,ep1,ep2);
  967.                 }
  968. coexit: *node = ep1;
  969.         return tp1;
  970. }
  971.  
  972. TYP     *expression(node)
  973. /*
  974.  *      evaluate an expression where all operators are legal.
  975.  */
  976. struct enode    **node;
  977. {       TYP     *tp;
  978.         tp = commaop(node);
  979.         if( tp == 0 )
  980.                 *node = 0;
  981.         return tp;
  982. }
  983.  
  984.